//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
var WxParse = require('../../wxParse/wxParse.js');
Page({
    data: {
        goods: {},
        show: false,
        selectZhi: 0,
        value: '',
        heizhi: 300,
        prices: [{
                name: '自定义',
            },
            {
                name: '加价5%',
            },
            {
                name: '加价10%',
            }, {
                name: '加价20%',
            }
        ],

        sum: 0,
        haha: '',
        showXin: false,

        isShow: false,
        recArr: [],
        buttonSty: false, //去结算
        shelveNo: '',
        sum: 0,
        wode: '',
        custNo: '',
        custN: '',
        skuNo: '',
        showShre: false,
        itemZhi: {},
        showButton: true,
        showKe: false,
        userStatus: {
            'ckName': '',
            'ckType': ''
        },
        msgList: [{
                context: "三三**，李西**，记得**，正在下单"

            },
            {
                context: "刘*，小可*，待定**，正在下单",

            },
            {
                context: "诸**，小小*，不可*，正在进行下单",

            },
            {
                context: "孙*，李*，无天， 正在下单",

            },
            {
                context: "孙*，李*，无天，正在下单",

            },
            {
                context: "散散和海**，李*，刘提阿**， 正在下单",

            },
            {
                context: "筋骨*，李是拒绝*，天天冉ran*， 正在下单",

            },
            {
                context: "和海*，不回头*，吐古*， 正在下单",

            },
            {
                context: "瞽*，等不及*，无*， 正在下单",

            },
            {
                context: "结局*，李*，wix*， 正在下单",

            },
            {
                context: "wxj*，all*，WiFi*， 正在下单",

            },
            {
                context: "看聚*，悠然一缝*，捷尔*， 正在下单",

            },
            {
                context: "仙女你*，数据*，发*， 正在下单",

            },
            {
                context: "数据*，以后让*，老爱*， 正在下单",

            },
            {
                context: "WiFi*，嗯哦哦*，WiFi*， 正在下单",

            },

            {
                context: "孙非*，IEIE*，为自己*， 正在下单",

            },
            {
                context: "湖**，信念*，scjjcsjc*， 正在下单",

            }
        ],

    },
    onLoad: function(options) {
        let that = this;

        if (options.source == "shopPage") {
            this.setData({
                custN: options.custNo,
                skuNo: options.skuNo,
                showButton: false,
            })

            // this.setData({
            //     custN: 'CK2021012812647631688704',
            //     skuNo: 'sku11418087376896',
            //     showButton:false,
            // })
            //'CK2021012812647631688704/sku11418087376896'
            let str = options.custNo + '/' + options.skuNo;
            Api.getSkGoodsInfoApi(str).then(res => {
                var a = res.data.goodsIntroduce;
                if (a != null) {
                    WxParse.wxParse('article', 'html', a, that, 5);

                }
                that.setData({
                    goods: res.data,
                })
            })

        } else {
            if (options.hasOwnProperty('custNo')) {

                app.globalData.custNo = options.custNo;
                this.setData({
                    wode: options.item,
                    custNo: options.custNo,
                })

                Api.getGoodsDetailApi({
                    'skuNo': options.item
                }).then(res => {
                    if (res.code == 0) {
                        var a = res.data.goodsIntroduce;
                        if (a != null) {
                            WxParse.wxParse('article', 'html', a, that, 5);

                        }
                        that.setData({
                            goods: res.data,
                        })
                    }
                })

            } else {
                var bean = {};
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.on('acceptDataFromOpenerPage', function(data) {

                    bean = data.data;
                    that.setData({
                        wode: data.data.skuNo,
                    })
                    Api.getGoodsDetailApi({
                        'skuNo': bean.skuNo
                    }).then(res => {
                        if (res.code == 0) {
                            var a = res.data.goodsIntroduce;
                            if (a != null) {
                                WxParse.wxParse('article', 'html', a, that, 5);
                            }
                            that.setData({
                                goods: res.data,

                            })
                        }
                    })

                })
            }
        }

    },
    catchTouchMove: function(res) {
        return false
    },
    onShow() {
        // this.setData({

        //     heizhi:app.globalData.screenHeight/2+200
        // })
        let a = wx.getStorageSync('baseInfo');
        if (a.hasOwnProperty('ckType')) {
            this.setData({
                userStatus: a
            })
        } else {
            this.setData({
                userStatus: {
                    'ckName': '',
                    'ckType': ''
                },
            })
        }
        let myCust = wx.getStorageSync('myCustNo');
        if (myCust.length > 5) {
            this.setData({
                showShre: true
            });

            // return  wx.showToast({ title: "请先登录！", icon: 'none' });
        } else {
            this.setData({
                showShre: false
            });
        }
        // var shre = wx.getStorageSync('share');

        // if (shre != null && shre == 'share') {
        //     this.setData({
        //         show: true
        //     });
        // }
    },
    ZhaoHuo: function() {
        wx.switchTab({
            url: '../GoodsLook/index'
        })
    },
    GOhOME() {
        wx.switchTab({
            url: '../index/index'
        })

        // wx.showToast({
        //     title: '请先登录再进行分享',
        //     icon: 'none'
        // });
    },

    backHome: function() {
        wx.navigateBack();
    },
    saveGroup() {
        let openId = wx.getStorageSync('openId');

        if (!openId) {
            wx.navigateTo({
                url: '../login/index'
            })
        } else {
            wx.navigateTo({
                url: '../DuanZhang/index'
            })
        }

    },
    callm() {
        let openId = wx.getStorageSync('openId');

        if (!openId) {
            wx.navigateTo({
                url: '../login/index'
            })
        } else {


            let i = this.data.goods;
            let j = this.data.custN;



            wx.navigateTo({
                url: '../CustXiaDan/index',
                success: function(res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', {
                        data: i,
                        shopkeeperCkNo: j
                    })
                }
            })
        }
    },
    previewImage: function(e) {
        var current = this.data.goods.wechatQrCode;
        wx.previewImage({
            current: current,
            urls: [current]
        })
    },

    sure() {
        wx.setStorageSync("share", "")
        this.setData({
            show: false
        });
    },
    onShareAppMessage: function() {


        let that = this;
        var sell = '';
        if (this.data.value == '') {
            sell = this.data.goods.goodsPrice;
        } else {
            sell = this.data.value;
        }
        let data = {
            "goodsNo": this.data.goods.goodsNo,
            "remark": "",
            "sellPrice": sell,
            "shelveName": '',
            "shelveNo": '',
            "skuNo": this.data.goods.skuNo,
            "state": "100001"
        }
        Api.saveGoodsShelvesApi(data).then(res => {
            if (res.code == 0) {
                // console.log(res.data.shelveNo);
                //  console.log('11');
                that.setData({
                    shelveNo: res.data.shelveNo,
                })
            }
        })
        let a = that.data.goods.attUrl;
        let showBu = that.data.showButton;
        let skuNo;
        let cust;
        if (showBu == false) {
            skuNo = that.data.skuNo;
            cust = that.data.custN;
        } else {
            cust = wx.getStorageSync('ckNo');
            skuNo = that.data.goods.skuNo;
        }



        // setTimeout({

        // })

        // debugger
        //  console.log(that.data.shelveNo);
        //  debugger
        let name = this.data.goods.goodsName;
        return {

            title: name,
            path: `/pages/shopDetail/index?source=shopPage&skuNo=` + skuNo + `&custNo=` + cust,
            imageUrl: a,

            success: function(res) {
                // 转发成功之后的回调
                if (res.errMsg == 'shareAppMessage:ok') {}
            }
        }

    },


    jumpC: function() {
        if (this.data.showShre) {
            var str = this.data.goods;
            wx.navigateTo({
                url: '../shouliren/index',
                success: function(res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', {
                        data: str,
                        type: '111801',
                        fenzhu: false
                    })
                }
            })

        } else {
            wx.navigateTo({
                url: '../login/index',

            })

            // wx.showToast({ title: "请先登录！", icon: 'none' });
        }



    },

    onClose() {
        this.setData({
            showXin: false,
            showKe: false,
            show: false,
        });
    },
    calld() {
        this.setData({
            showKe: true,
        });

    },
    fuzhi: function() {
        let a = this.data.goods.phoneNo;
        wx.makePhoneCall({
            phoneNumber: a,
            success: function() {

            },
            fail: function() {

                // console.log('拨打失败')
            }
        })
        // wx.setClipboardData({  data: this.data.goods.wechatNo,})

    },

    onSelect(event) {
        let ite = this.data.goods;

        let a = event.detail.name;


        wx.navigateTo({
            url: '../shouliren/index',
            success: function(res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                    data: ite,
                    type: '111801'
                })
            }
        })


    },



    jumpG: function() {
        wx.switchTab({
            url: '../cart/index',
        })
    },
    clickSC: function() {
            if (!this.data.showShre) {
                return wx.navigateTo({
                    url: '../login/index'
                })
            }

            let that = this;
            var boll = this.data.goods;
            if (boll.clNo != null) {
                Api.deleSCApi(boll.skuNo).then(res => {
                    if (res.code == 0) {
                        that.setData({
                            ['goods.clNo']: null
                        })
                    }
                })

            } else {
                let data = {
                    "busiType": "109601",
                    "evNo": '',
                    "goodsNo": this.data.goods.goodsNo,
                    "skuNo": this.data.goods.skuNo
                }
                Api.saveSCApi(data).then(res => {
                    if (res.code == 0) {
                        that.setData({
                            ['goods.clNo']: 1
                        })
                    }

                })

            }
        }

        ,

    shareMai: function() {
        // console.log('1');
        // wx.showShareMenu({
        //   withShareTicket: true,

        // })
        this.setData({
            showXin: true,
        })

    },
    addHj: function() {
        this.setData({
            show: true
        })

    },
    addHj1: function() {
        let that = this;
        var sell = '';
        if (this.data.value == '') {
            sell = this.data.goods.goodsPrice;
        } else {
            sell = this.data.value;
        }
        let data = {
            "goodsNo": this.data.goods.goodsNo,
            "remark": "",
            "sellPrice": sell,
            "shelveName": '',
            "shelveNo": this.data.shelveNo,
            "skuNo": this.data.goods.skuNo,
            "state": "100001"
        }
        Api.saveGoodsShelvesApi(data).then(res => {
            if (that.data.shelveNo == '') {
                wx.showToast({
                    title: '加入货架成功!',
                    icon: 'none'
                });
            } else {
                wx.showToast({
                    title: '修改货架商品成功!',
                    icon: 'none'
                });
            }
            if (res.code == 0) {
                that.setData({
                    shelveNo: res.data.shelveNo
                })
            }


        })

    },

    clickSelectGuoge: function(e) {

        let item = e.currentTarget.dataset.item;
        let zhi = codeZ.baifenSum(this.data.goods.goodsPrice, item);

        this.setData({
            selectZhi: e.currentTarget.dataset.index,
            value: zhi
        })
    },

    clickTap: function() {
        let a = this.data.goods.goodsNo;
        wx.navigateTo({
            url: '../faQuan/index',
            success: function(res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                    data: a
                })
            }
        })



    },
    onChange(event) {
        // event.detail 为当前输入的值

        console.log(event.detail);
        this.setData({

            value: event.detail,
        });
    },


})
